//
//  HeaderDragController.swift
//  CTCollectionViewDemo
//
//  Created by GodCoder on 2019/8/6.
//  Copyright © 2019 CTCollectionView. All rights reserved.
//

import UIKit

class HeaderDragController: UIViewController {

    lazy var collectionView: UICollectionView = {
        let bgImages = UIImageView.init(frame: self.view.frame)
        bgImages.image = UIImage.init(named: "5")
        
        let layout = HeaderDragLayout.init()
        layout.itemSize = CGSize(width: self.view.bounds.width-20, height: self.view.bounds.height/2)
        layout.minimumLineSpacing = 10
        layout.minimumInteritemSpacing = 10
        layout.sectionInset = UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5)
        layout.headerReferenceSize = CGSize(width: view.bounds.width, height: 200)
        
        let cv = UICollectionView.init(frame: self.view.frame, collectionViewLayout: layout)
        cv.delegate = self
        cv.dataSource = self
        cv.backgroundView = bgImages
        
        cv.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "UICollectionViewCellID1")
        cv.register(UINib.init(nibName: "ImageHeader", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "UICollectionReusableViewID1")
        return cv
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.addSubview(collectionView)
    }
}

extension HeaderDragController: UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 3
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "UICollectionViewCellID1", for: indexPath)
        cell.backgroundColor = UIColor.init(named: "Grey")
        
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        if kind == UICollectionView.elementKindSectionHeader {
            let header = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "UICollectionReusableViewID1", for: indexPath) as! ImageHeader
            header.imageBgView.image = UIImage.init(named: "10")
            return header
        }
        return UICollectionReusableView.init()
    }
    
}

extension HeaderDragController: UICollectionViewDelegate {
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
        return CGSize(width: view.bounds.width, height: 200)
    }
}
